/**
 * 借支
 * @author  Jello
 * @create  2018/12/06 16:21
 * @version 1.0
 * @company 广东蓝聘科技网络有限公司
 **/
var spinner = new Spinner(opts);
var companyId = "";
var companyName = "";
var userid_sign = "";
var page = 1;
var size = 20;
var token = "";
var spinner = new Spinner(opts);
var openid = "";
var countentPhone = "";
var userName = "";
var userUrl = "";

$(function() {
	openid = getParam("openid"); //微信openid
	countentPhone = getParam("countentPhone"); //手机框 0弹出1不弹出
	userName = getParam("userName"); //用户昵称
	userUrl = getParam("userUrl"); //用户头像
	if(openid != "" && openid != "null" && openid != null) {
		sessionStorage.setItem("openid", openid);
	}
	if(userName != "" && userName != "null" && userName != null) {
		sessionStorage.setItem("weixin_userName", decodeURI(userName));
	}
	if(userUrl != "" && userUrl != "null" && userUrl != null) {
		sessionStorage.setItem("weixin_userUrl", userUrl);
	}

	if(countentPhone == "0") {
		$(".phonecover").show();
		$(".phonebox").show();
	} else {
		sessionStorage.setItem("lend_sign", 001199);
		if(sessionStorage.getItem("lend_type") != 1) {
			//查询是否有借支记录
			var lendParam = {};
			lendParam.userid_sign = sessionStorage.getItem("openid");
			queryLendRecord(lendParam, function(resultObj) {
				if(resultObj.data) {
					window.location.href = "app/lend/lend_detail.html";
				}
			})
		}

		//查询可借支公司列表
		var param = {};
		param.openid = sessionStorage.getItem("openid");
		param.page = page;
		param.size = size;
		queryLendCompanyList(param, function(resultObj) {
			if(resultObj.code == 0 && resultObj.data.length > 0) {
				$(".companycon").empty();
				for(var i = 0; i < resultObj.data.length; i++) {
					var html = '<div class="companylist" onclick="selectCompany(' + resultObj.data[i].id + ',\'' + resultObj.data[i].mechanismName + '\')" >' +
						'<span class="listname">' + resultObj.data[i].mechanismName + '</span></div>';
					$(".companycon").append(html);
				}
			}
		});
	}

	//获取验证码
	$("#code_btn").click(function() {
		var phoneNum = $("#phoneNum").val(); //手机号
		var flag = checkPhone(phoneNum);
		if(flag) {
			var param = {};
			param.phone = phoneNum;
			param.type = 6;
			//获取验证码接口上送
			gainCode(param, function(data) {
				token = data;
				//开启验证码定时器
				for(i = 1; i <= 60; i++) {
					if(i == 1) {
						window.setTimeout("countDown(" + i + ")");
					} else {
						window.setTimeout("countDown(" + i + ")", (i - 1) * 1000);
					}
				}
			});
		}
	})

	//手机号绑定确定
	$("#phone_btn").click(function() {
		var phone = $("#phoneNum").val(); //手机号
		var code = $("#code").val();
		//校验验证码接口上送
		var param = {};
		param.phone = phone;
		param.type = 6;
		param.code = code;
		param.token = token;
		mateCode(param, function() {
			var param = {};
			param.userid_sign = sessionStorage.getItem("openid");
			param.phone = phone;
			param.userName = sessionStorage.getItem("weixin_userName");
			param.userUrl = sessionStorage.getItem("weixin_userUrl");

			//微信绑定客户手机号
			bindCustomerPhone(param, function(resultObj) {
				closeSpin()
				$(".phonecover").hide();
				$(".phonebox").hide();

				sessionStorage.setItem("lend_sign", 001199);
				if(sessionStorage.getItem("lend_type") != 1) {
					//查询是否有借支记录
					var lendParam = {};
					lendParam.userid_sign = userid_sign;
					queryLendRecord(lendParam, function(resultObj) {
						if(resultObj.data) {
							window.location.href = "app/lend/lend_detail.html";
						}
					})
				}

				//查询借支公司列表
				var param = {};
				param.openid = sessionStorage.getItem("openid");
				param.page = page;
				param.size = size;
				queryLendCompanyList(param, function(resultObj) {
					if(resultObj.code == 0 && resultObj.data.length > 0) {
						$(".companycon").empty();
						for(var i = 0; i < resultObj.data.length; i++) {
							var html = '<div class="companylist" onclick="selectCompany(' + resultObj.data[i].id + ',\'' + resultObj.data[i].mechanismName + '\')" >' +
								'<span class="listname">' + resultObj.data[i].mechanismName + '</span></div>';
							$(".companycon").append(html);
						}
					}
				});
			});
		})
	})

	//屏幕滚动
	$(".companycon").scroll(function() {
		var scrollHeight = $(this)[0].scrollHeight; //滚动距离总长
		var scrollTop = $(this)[0].scrollTop; //滚动到的当前位置
		var divHeight = $(this).height(); //当前窗体的高度可見高度
		//		console.log("滚动距离总长:" + scrollHeight + "滚动到的当前位置:" + scrollTop + "当前窗体的高度可見高度:" + scrollHeight);
		if(scrollTop + divHeight >= scrollHeight) {
			flipPage();
		}
	});

	//跳转绑定银行卡
	$("#yes_btn").click(function() {
		$(".esc_divbox").hide();
		$(".esc_box").hide();
		window.location.href = "paycardmanager_grade.html";
	})

	//查询可借支公司
	$("#company").click(function() {
		$(".companybox").show();
		$(".cover").show();
	})

	//取消
	$("#cancel_btn").click(function() {
		$(".companybox").hide();
		$(".cover").hide();
	})

	//确定
	$("#sure_btn").click(function() {
		$(".companybox").hide();
		$(".cover").hide();
		if(companyName == "" || companyName == undefined) {
			mui.toast("请选择公司");
			return;
		} else {
			$("#company").val(companyName);
			//查询是否允许借支
			$.ajax({
				type: "get",
				url: serverUrl + "/lendmoney/query_isLend_api?sgin=" + sessionStorage.getItem("openid") + "&mechanismId=" + companyId,
				cache: false,
				async: false,
				contentType: "application/json; charset=utf-8",
				dataType: "json",
				timeout: 15000,
				crossDomain: true,
				beforeSend: function() {
					$("#myspin").text("");
					var target = $("#myspin").get(0);
					spinner.spin(target);
				},
				success: function(data) {
					var resultObj = eval(data);
					closeSpin(100);
					if(resultObj.data.res_code == 0) {
						$(".lend_limit .limit_num").css({
							"font-size": " 0.44rem",
							"line-height": " 0.44rem"
						});
						$("#balance").html(toDecimal(resultObj.data.lendMoney));
					} else if(resultObj.data.res_code == 20010) {
						$(".esc_divbox").show();
						$(".esc_box").show();
					} else {
						mui.toast(resultObj.msg);
						return;
					}
				},
				error: function(XMLHttpRequest, status, errorThrown) {
					closeSpin(100);
					if(status == 'timeout') {
						mui.toast("请求超时");
					} else {
						mui.toast("请求网络服务数据失败");
					}
					return;
				}
			});
		}
	})

	//借支
	$("#lend_button").click(function() {
		var money = $("#lendMoney").val();
		var teacherTel = $("#teacherTel").val();
		var flag = (checkCompany() && checkPhone(teacherTel) && checkMoney());
		if(flag) {
			//查询借支记录
			var lendParam = {};
			lendParam.userid_sign = sessionStorage.getItem("openid");
			queryLendRecord(lendParam, function(resultObj) {
				if(resultObj.data) {
					if(resultObj.data.status == "0") {
						mui.toast("您已有一笔待审核的借支");
						return;
					} else {
						var param = {};
						param.lendMoney = money;
						param.mechanismId = companyId;
						param.openid = sessionStorage.getItem("openid");
						param.teacherTel = teacherTel;
						addLendMoney(param, function(resultObj) {
							if(resultObj.code == 0 && resultObj.data == 1) {
								window.location.href = "app/lend/lend_detail.html";
							} else {
								mui.toast(resultObj.msg);
								return;
							}
						});
					}
				} else {
					var param = {};
					param.lendMoney = money;
					param.mechanismId = companyId;
					param.openid = sessionStorage.getItem("openid");
					param.teacherTel = teacherTel;
					addLendMoney(param, function(resultObj) {
						if(resultObj.code == 0 && resultObj.data == 1) {
							window.location.href = "app/lend/lend_detail.html";
						} else {
							mui.toast(resultObj.msg);
							return;
						}
					});
				}
			})
		}
	})
})

/**
 * 检验选择公司
 */
function checkCompany() {
	var company = $("#company").val();
	if(company == "" || company == undefined) {
		mui.toast("请选择公司");
		return;
	}
	return true;
}

function checkMoney() {
	var money = $("#lendMoney").val();
	var balance = $("#balance").html();
	var reg = /^[0-9]*[1-9][0-9]*$/;
	if(money == "" || money == null) {
		mui.toast("请输入借支金额");
		return;
	} else if(!reg.test(money)) {
		mui.toast("请输入正确的整数金额");
		return;
	} else if(eval(money) > eval(balance)) {
		mui.toast("借支金额超过可借支额度");
		return;
	} else if(eval(balance) == 0) {
		mui.toast("暂时不可借支");
		return;
	}
	return true;
}

/**
 * 翻页查询借支公司
 */
function flipPage() {
	page++;

	//查询借支公司列表
	var param = {};
	param.openid = sessionStorage.getItem("openid");
	param.page = page;
	param.size = size;
	queryLendCompanyList(param, function(resultObj) {
		if(resultObj.code == 0 && resultObj.data.length > 0) {
			for(var i = 0; i < resultObj.data.length; i++) {
				var html = '<div class="companylist" onclick="selectCompany(' + resultObj.data[i].id + ',\'' + resultObj.data[i].mechanismName + '\')" >' +
					'<span class="listname">' + resultObj.data[i].mechanismName + '</span></div>';
				$(".companycon").append(html);
			}
		}
		var length = resultObj.data.length;
		endPage(length);
	});
};

function endPage(length) {
	if(length % size != 0 || length == 0) {
		setTimeout(function() {
			mui.toast('已全部加载');
		}, 500);
	}
}

/**
 * 选择借支公司
 * @param {Object} key
 * @param {Object} value
 */
function selectCompany(key, value) {
	companyId = key;
	companyName = value;
}